import useMain from "@/stores/useMain.js";
import { mapState } from 'pinia';
import Api from "./api";
import {App} from "@lowcode/jpaas-share-component";
export default {
    mixins:[App],
    computed:{
        ...mapState(useMain,['curApp','navigations']),
    },
    data(){
        return {
            checked:[],
            treeData:[],
            expandedKeys:[],
            childrenData: [],
            isLoading:false
        }
    },
    created() {
        this.getTreeCatByKey();
    },
    methods:{
        getTreeCatByKey(){
            let self_ = this ;
            Api.getTreeCatByKey(this.catKey).then(res=>{
                let treeCat = res;
                treeCat.catKey = treeCat.key;
                treeCat.treeId = "0";
                treeCat.parentId = "-1";
                treeCat.key = "0";
                treeCat.value = "0";
                treeCat.children = [];
                treeCat.title = treeCat.name;
                treeCat.slots = {icon: 'smile'};
                self_.treeData=[]
                self_.treeData.push(treeCat);
                //默认展开第一个；
                self_.expandedKeys.push(treeCat['treeId']);
                if(self_.treeRootList && self_.treeRootList.length>0){
                    self_.treeRootKey(true,treeCat);
                }else{
                    self_.triggerClick(true,treeCat,self_.treeData);
                }

            })
        },
        treeRootKey(trigger,tree){
            let appIds=[];
            for(let i=0;i<this.treeRootList.length;i++){
                let treeCat=this.treeRootList[i];
                treeCat.treeCatType="rootApp";
                treeCat.catKey = tree.catKey;
                treeCat.treeId = treeCat.appId;
                treeCat.parentId = tree.treeId;
                treeCat.key = treeCat.appId;
                treeCat.value = treeCat.appId;
                treeCat.children = [];
                treeCat.slots = {icon: 'smile'};
                tree.children.push(treeCat);
                appIds.push({
                    appId:treeCat.appId,
                    isAdmin:treeCat.isAdmin
                });
            }
            this.triggerRootClick(tree,JSON.stringify(appIds));
        },
        transData(data,name,key){
            if(!data){
                data=[];
                return;
            }
            for (let i = 0; i < data.length; i++) {
                var row = data[i];
                this.handRow(row,name,key);
            }
        },
        handRow(row,name,key){
            var isLeaf=row.childAmount==0;
            var obj={ title: row[name],  key: row[key],isLeaf:isLeaf };
            Object.assign(row,obj)
        },
        async triggerRootClick(tree,appIds){
            let self_ = this ;
            let res = "",catKey = tree.catKey ;
            res =await Api.getAllTopNodesByCatKey(this.catKey, this.readKey, this.getAppIsGrant(), appIds, encodeURIComponent(JSON.stringify(this.statisticConfig)));
            self_.handleRootTreeData(tree,res);
            tree.loading=false;
            self_.treeData = [...self_.treeData];
        },
        handleRootTreeData(tree,res){
            for(let i=0;i<tree.children.length;i++){
                let children=tree.children[i];
                let appId= children.appId;
                let childrenList= res[appId];
                let _data = [...childrenList] ;
                this.transData(_data, "name", "treeId");
                children['children'] = _data ;
            }
        },
        getAppIsGrant(){
            if( this.isMyBpm){
                return  false;
            }
            return this.isGrant;
        },
        getAppIsAdmin(tree){
            if( this.isAdmin){
                return  this.isAdmin;
            }
            if(!this.treeRootList || this.treeRootList.length==0){
                return false;
            }
            let rootApp = this.treeRootList.find(item =>item.appId==tree.appId);
            if(rootApp && rootApp.isAdmin){
                return  true;
            }
            return false;
        },
        async triggerClick(trigger,tree,parenData){
            let self = this ;
            if(!trigger){ return }
            if(tree.children && tree.children.length > 0){
                return ;
            }else {
                let res = "",catKey = tree.catKey ;
                if(Array.isArray(parenData)){
                    res =await Api.getTopByCatKey(this.catKey, this.readKey, this.isAdmin, this.isGrant, this.curApp.appId, encodeURIComponent(JSON.stringify(this.statisticConfig)),this.isBpmClient,this.isMyBpm);
                    let treeParentData = parenData.find(item=>{return item.key == tree.key})
                    let _data = [...res] ;
                    this.transData(_data, "name", "treeId");
                    treeParentData['children'] = _data ;
                }else {
                    let appId = (!tree.appId || "defaultTree"==tree.appId)?"":tree.appId;
                    let treeId = "rootApp"==tree.treeCatType?"0":tree.treeId;
                    let  isAdmin=this.getAppIsAdmin(tree);
                    res =await Api.getCatByParentId(treeId, catKey, this.readKey, isAdmin, this.isGrant, encodeURIComponent(JSON.stringify(this.statisticConfig)),this.isBpmClient,appId,this.isMyBpm);
                    if(res){
                        let _data = [...res];
                        this.transData(_data, "name", "treeId");
                        tree['children'] = _data ;
                    }
                }
                tree.loading=false;
                self.treeData = [...self.treeData];
            }
        },
    }   ,
    watch:{
        treeRootList() {
            this.treeData=[]
            this.getTreeCatByKey()
        }
    }
}
